package com.feedgraph;

import java.io.File;
import java.io.IOException;

import com.feedgraph.FeedManager.BasicFeed;
import com.feedgraph.FeedManager.Feed;

import android.net.http.HttpResponseCache;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Loader;
import android.util.Log;
import android.view.Window;

/**
 * Example Activity which uses {@link AsycFeedLoader} to display {@link FeedItem}s
 * @author ryanne
 *
 */
public class FeedViewActivity extends ListActivity implements FeedManager.LoaderCallbacks {
	
	static final int LOADER_ID = 0;
	
	protected FeedManager mFeedManager;
	protected FeedAdapter mAdapter;

	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		requestWindowFeature (Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView (android.R.layout.list_content);
		
		mFeedManager = new FeedManager(this);
		mAdapter = new FeedListAdapter(this);
		setListAdapter(mAdapter);
		
		getLoaderManager().initLoader(LOADER_ID, null, this);
		
		try {
			File httpCacheDir = new File (getCacheDir (), "http");
			long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
			HttpResponseCache.install (httpCacheDir, httpCacheSize);
		}
		catch (IOException e) {
			Log.i ("HttpResponseCache", "HTTP response cache installation failed:"
					+ e);
		}
	}

	@Override
	public void onStop () {
		HttpResponseCache cache = HttpResponseCache.getInstalled ();
		if (cache != null) {
			cache.flush ();
		}
		
		super.onStop ();
	}

	/** overload this method to specify your own feed sources. 
	 * 
	 */
	@Override
	public Loader<BasicFeed> onCreateLoader (int id, Bundle args) {
		return mFeedManager.makeLoader ("/test/*/*");
	}

	@Override
	public void onLoadFinished (Loader <BasicFeed> arg0, BasicFeed feed) {
		Log.d("FeedViewActivity", "finished loading");
		mAdapter.setFeed (feed);
	}

	@Override
	public void onLoaderReset (Loader <BasicFeed> arg0) {
		mAdapter.setFeed (null);
		
	}

}
